﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace ZK.Common
{
    public class FileUpload
    {


        #region 上传抓的图片
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="errorStr"></param>
        /// <returns></returns>
        public static string HttpUploadFile(string filePath, out string errorStr)
        {
            string ret = "";
            errorStr = "";
            bool b = false;
            Encoding encoding = Encoding.UTF8;
            string boundary = DateTime.Now.Ticks.ToString("x");
            byte[] boundarybytes = Encoding.ASCII.GetBytes("\r\n----" + boundary + "\r\n");
            byte[] endbytes = Encoding.ASCII.GetBytes("\r\n----" + boundary + "--\r\n");

            Dictionary<string, string> data = new Dictionary<string, string>();
            data.Add("moudle", "yhp"); //传文件存储路径
            //data.Add("systemType", "RoadGateRecord"); //传文件存储路径
            //data.Add("filePath", "RoadGateRecord/" + DateTime.Now.ToString("yyyyMMdd"));
            string url = InterfaceExecute.GetMediaUrl("fileUploadSave", data);

            try
            {
                //1.HttpWebRequest
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.ContentType = "multipart/form-data; boundary=--" + boundary;
                request.Method = "POST";
                request.KeepAlive = true;
                request.Credentials = CredentialCache.DefaultCredentials;
                //request.Timeout = 10000;

                using (Stream stream = request.GetRequestStream())
                {
                    string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type:application/octet-stream\r\n\r\n";
                    byte[] buffer = new byte[4096];
                    int bytesRead = 0;
                    stream.Write(boundarybytes, 0, boundarybytes.Length);
                    string header = string.Format(headerTemplate, "file", Path.GetFileName(filePath));
                    byte[] headerbytes = encoding.GetBytes(header);
                    stream.Write(headerbytes, 0, headerbytes.Length);
                    using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                    {
                        while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
                        {
                            stream.Write(buffer, 0, bytesRead);
                        }
                    }
                    stream.Write(endbytes, 0, endbytes.Length);
                }
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                ret = HttpClientUtil.getResponseString(response);
                JObject ja = InterfaceExecute.getJobj(out errorStr, b, ret);
                if (errorStr == "" && ja != null)
                {
                    return ja["data"].ToString();
                }
            }
            catch (Exception ex)
            {
                Common.Log4.LogManage.WriteErr("上传抓拍图片返回数据为[" + ret + "],异常信息为:" + ex.Message);
                errorStr = ex.Message;
            }
            return "";
        }

        #endregion


    }
}
